<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="javax.jdo.Query" %>
<%@ page import="com.gestordeavisos.Aviso" %>
<%@ page import="com.gestordeavisos.PMF" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<title>Gestor de Avisos de Actividades</title>
    <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
    
</head>
<!-- onload='response.encodeURL(getServletContext.getContextPath) + "/gestordeavisos"'
Llamar a Servlet que haga lo de la Hash -->
<body>
<div id="arriba">
<p>En Construcción <img src="/imagenes/obras.png" border="0"></p>
<%
	UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    if (user == null) {
%>

<p id="login">Hola!
<a href="<%=userService.createLoginURL(request.getRequestURI())%>">Identificate 
para poder acceder</a>.</p>

<%
	} else {
%>
<p id="login">Hola, <%=user.getNickname()%>! (
<a href="<%=userService.createLogoutURL(request.getRequestURI())%>"> Salir </a>).</p>
</div>
	<!-- Contenedor -->
	<div id="container">
		<!-- Contenedor inicio -->
		<div id="inicio">
			<div id="logo">
				<img src="/imagenes/logo.gif" alt="Logo" border="0">
			</div>
			<div id="login">
        		
            </div>
        </div>
		<!-- Gestor de avisos (CRUD) -->
		
		<div>
		<table>
		<tr>
		<td>
			<!-- Panel de la izquierda -->
			<div id="izquierda">
				<div id="button">
				<form>
					<input type="button" value="Nuevo Aviso de Actividad" onclick = "window.location.href='nuevoaviso.jsp'"></input>
				</form>
				</div>
			</div>
		</td>
		<td>
			<!-- Panel de la derecha -->
			<table border="1">
				<tr>
				<td>
					<table>
					<thead>
						<tr>
						<th>Eliminar</th>
						<th>Actividad</th>
						<th>Nº Veces</th>
						<th>Hora del día</th>
						<th>Autor</th>
						<th>Fecha de creación</th>
						<th>Repeticion</th>						
						</tr>
					</thead>
					</table>
				</td>
				<td>
					<table><tr><th>Editar</th></tr></table>	
				</td>			
				</tr>
						
					<%
					PersistenceManager pm = PMF.get().getPersistenceManager();
					String select_query = "select from " + Aviso.class.getName(); 
					Query query = pm.newQuery(select_query); 
					query.setFilter("autor == paramAutor"); 
					query.declareParameters("java.lang.String paramAutor"); 
					List<Aviso> avisos = (List<Aviso>) query.execute(user);
					if (avisos.isEmpty()) {
					%>
					<tr>
						<p>No hay actividades programadas.</p>
					</tr>
					<%
					}
					else{
							    	
					for (Aviso a : avisos) {
					%>
					<tr>
						<td>
						<form id="eliminar_aviso" action="/eliminaraviso" method="post" >
						<input type="hidden" name="key" value="<%=a.getId()%>"/>
						<table id="lista_avisos">
							<tr id="fila">
								<td><input type="image" src ="/imagenes/delete.png" /></td>
								<td><%=a.getActividad()%></td> 
								<td><%=a.getCuanto()%></td>
								<% for(int j=0;j<Integer.valueOf(a.getCuanto());j++){%>
								<td><%=a.getCuando().get(j).toString()%></td>
								<%} %>
								<td><%=a.getAutor().getNickname()%></td>
								<td><%=a.getFecha()%></td>
								<% if(a.getRepeticion()=="si"){%>
								<td><input readonly="readonly" type="checkbox" checked="checked"></input></td>
								<%} %>
								<% if(a.getRepeticion()=="no"){ %>
								<td><input readonly="readonly" type="checkbox"></input></td>
								<%} %>
							</tr>
						</table>
						</form>	
					</td>
					<td>
						<form i	d="recoger_aviso" action="/recogeraviso" method="post">
						<input type="hidden" name="key" value="<%=a.getId()%>"/>
						<input id="modificar" type="image" src="/imagenes/modificar.gif" id="modificar" />
						</form>
					</td>
					</tr>
					
				<% } 
				pm.close();%>
				
				</table>
		</td>
		</tbody>
		</table>
		</div>
		</div>
		<!-- Parte de abajo -->
		<div id="abajo">
		<img src="http://code.google.com/appengine/images/appengine-noborder-120x30.gif" alt="Con la tecnología de Google App Engine" />
		</div>
	<%
    }}
%>
</body>
</html>
